package com.sensetime.jv.controller;

import com.sensetime.jv.annotation.RequirePermission;
import com.sensetime.jv.entity.ReturnWrapper;
import com.sensetime.jv.service.UserRoleService;
import com.sensetime.jv.vo.request.UserRoleEditRequestVO;
import com.sensetime.jv.vo.request.UserRoleRequestVO;
import com.sensetime.jv.vo.response.UserRoleRespVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user-role")
@Slf4j
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    // 添加用户角色关联
    @PostMapping("/add")
    @RequirePermission(description = "添加用户角色关联")
    public ReturnWrapper addUserRole(@Valid@RequestBody UserRoleRequestVO requestVO) {
        userRoleService.addUserRole(requestVO);
        return ReturnWrapper.success();
    }

    // 删除用户角色关联
    @DeleteMapping("/delete/{userId}/{roleId}")
    @RequirePermission(description = "删除用户角色关联")
    public ReturnWrapper deleteUserRole(@PathVariable Integer userId, @PathVariable Integer roleId) {
        userRoleService.deleteUserRole(userId, roleId);
        return ReturnWrapper.success();

    }

    // 修改用户角色关联
    @PutMapping("/update")
    @RequirePermission(description = "修改用户角色关联")
    public ReturnWrapper<Object> updateUserRole(@Valid @RequestBody UserRoleEditRequestVO requestVO) {
        userRoleService.updateUserRole(requestVO);
        return ReturnWrapper.success();

    }

    // 查询用户的角色关联
    @GetMapping("/list")
    @RequirePermission(description = "查询用户的角色关联")
    public ReturnWrapper<List<UserRoleRespVO>> getUserRoles() {
        return ReturnWrapper.success(userRoleService.getUserRoles());

    }
}